package com.qnbc.mystery.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qnbc.mystery.domain.BoxInfo;
import com.qnbc.mystery.domain.BoxSet;
import com.qnbc.mystery.pojo.domain.FoodsProduct;
import com.qnbc.mystery.pojo.domain.ProductSwiperImage;
import com.qnbc.mystery.service.IBoxInfoService;
import com.qnbc.mystery.service.IBoxSetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qnbc.mystery.mapper.ProductSwiperImageMapper;
import com.qnbc.mystery.service.IProductSwiperImageService;

/**
 * 产品对应的颜色分类Service业务层处理
 *
 * @author guzj
 * @date 2024-01-30
 */
@Service
public class ProductSwiperImageServiceImpl extends ServiceImpl<ProductSwiperImageMapper, ProductSwiperImage> implements IProductSwiperImageService {
    @Autowired
    private ProductSwiperImageMapper productSwiperImageMapper;


    @Autowired
    private IBoxSetService boxSetService;

    @Autowired
    private IBoxInfoService boxInfoService;

    /**
     * 查询产品对应的颜色分类
     *
     * @param id 产品对应的颜色分类主键
     * @return 产品对应的颜色分类
     */
    @Override
    public ProductSwiperImage selectProductSwiperImageById(Long id) {
        ProductSwiperImage productSwiperImage = productSwiperImageMapper.selectProductSwiperImageById(id);
        FoodsProduct foodsProduct = productSwiperImageMapper.getproduct(productSwiperImage);
        productSwiperImage.setFoodsProduct(foodsProduct);
        return productSwiperImage;
    }

    /**
     * 查询产品对应的颜色分类列表
     *
     * @param productSwiperImage 产品对应的颜色分类
     * @return 产品对应的颜色分类
     */
    @Override
    public List<ProductSwiperImage> selectProductSwiperImageList(ProductSwiperImage productSwiperImage) {
        List<ProductSwiperImage> productSwiperImages = productSwiperImageMapper.selectProductSwiperImageList(productSwiperImage);
        for (ProductSwiperImage swiperImage : productSwiperImages) {
            if (swiperImage.getProType().equals("2") || swiperImage.getProType().equals("3")) {
                BoxInfo boxInfo = boxInfoService.selectBoxInfoById(swiperImage.getProductid());
                if (boxInfo != null) {
                    swiperImage.setProName(boxInfo.getBoxName());
                }
            }
        }
        return productSwiperImages;
    }

    /**
     * 新增产品对应的颜色分类
     *
     * @param productSwiperImage 产品对应的颜色分类
     * @return 结果
     */
    @Override
    public int insertProductSwiperImage(ProductSwiperImage productSwiperImage) {
        List<BoxSet> boxSets = boxSetService.selectBoxSetList(new BoxSet());
        if (productSwiperImage.getProType().equals("2") || productSwiperImage.getProType().equals("3")) {
            //一番赏
            BoxSet boxSet1 = boxSets.stream().filter(boxSet -> boxSet.getBoxName().equals(productSwiperImage.getName())).findFirst().get();
            productSwiperImage.setSort(boxSet1.getNum());
            BoxInfo boxInfo = boxInfoService.selectBoxInfoById(productSwiperImage.getProductid());
            productSwiperImage.setProPrice(boxInfo.getBoxPrice());
        }
        productSwiperImage.setResidueStock(productSwiperImage.getStock());
        return productSwiperImageMapper.insertProductSwiperImage(productSwiperImage);
    }

    /**
     * 修改产品对应的颜色分类
     *
     * @param productSwiperImage 产品对应的颜色分类
     * @return 结果
     */
    @Override
    public int updateProductSwiperImage(ProductSwiperImage productSwiperImage) {
        List<BoxSet> boxSets = boxSetService.selectBoxSetList(new BoxSet());
        if (productSwiperImage.getProType().equals("2") || productSwiperImage.getProType().equals("3")) {
            //一番赏
            BoxSet boxSet1 = boxSets.stream().filter(boxSet -> boxSet.getBoxName().equals(productSwiperImage.getName())).findFirst().get();
            productSwiperImage.setSort(boxSet1.getNum());
            BoxInfo boxInfo = boxInfoService.selectBoxInfoById(productSwiperImage.getProductid());
            productSwiperImage.setProPrice(boxInfo.getBoxPrice());
        }
        return productSwiperImageMapper.updateProductSwiperImage(productSwiperImage);
    }

    /**
     * 批量删除产品对应的颜色分类
     *
     * @param ids 需要删除的产品对应的颜色分类主键
     * @return 结果
     */
    @Override
    public int deleteProductSwiperImageByIds(Long[] ids) {
        return productSwiperImageMapper.deleteProductSwiperImageByIds(ids);
    }

    /**
     * 删除产品对应的颜色分类信息
     *
     * @param id 产品对应的颜色分类主键
     * @return 结果
     */
    @Override
    public int deleteProductSwiperImageById(Long id) {
        return productSwiperImageMapper.deleteProductSwiperImageById(id);
    }

    @Override
    public List<ProductSwiperImage> listBoxInfo(BoxInfo boxInfo) {
        return productSwiperImageMapper.listBoxInfo(boxInfo);
    }
}
